/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package run;

import testutil.TestObjectiveFunction;
import simple.*;
import algorithm.BreedingSelector;
import algorithm.Environment;
import algorithm.ObjectiveFunction;
import algorithm.PopulationBreeder;
import algorithm.PopulationInitializer;
import junit.framework.TestCase;

/**
 *
 * @author Pierre
 */
public class RunSimpleGA extends TestCase {

    public void testSimpleGA() {
        final int nGenerations = 100;
        double mutationChance = 0.05;
        double mutationFraction = 0.05;
        int populationSize = 100;
        
        PopulationInitializer initializer = new BitStringPopulationInitializer(populationSize, 40);
        BreedingSelector selector = new RankRouletteWheelSelector();
        PopulationBreeder crossOverBreeder = new DefaultPopulationBreeder(
                selector,
                new CrossOverOperator());
        PopulationBreeder mutator = new MutationBreeder(mutationChance, mutationFraction);
        
        ObjectiveFunction obj = new TestObjectiveFunction();
        Environment environment = new Environment(obj,new DoubleArrayBitStringEncoder(8, 5));
        
        SimpleGeneticAlgorithm algo = new SimpleGeneticAlgorithm(
                initializer,
                new NumberOfGenerationsStopCriterion(nGenerations),
                crossOverBreeder,
                mutator,
                environment);
        algo.addGenerationReport(new LogGenerationReport());
        try {
            algo.run();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
